/**
 *
 * @param {*} 时间戳： 12345678xxx
 *
 * @return ['yyyy-mm-dd', 'yyyy-mm-dd hh', 'yyyy-mm-dd hh:mm:ss' ]
 */

export function dateValueFormat (val) {
  const year = val.getFullYear()
  let month = val.getMonth() + 1
  let day = val.getDate()
  let hour = val.getHours()
  let minute = val.getMinutes()
  let second = val.getSeconds()
  if (month >= 1 && month <= 9) { month = `0${month}` }
  if (day >= 1 && day <= 9) { day = `0${day}` }
  if (hour >= 0 && hour <= 9) { hour = `0${hour}` }
  if (minute >= 0 && minute <= 9) { minute = `0${minute}` }
  if (minute >= 0 && minute <= 9) { second = `0${second}` }
  return [
    `${year}-${month}-${day}`,
    `${year}-${month}-${day} ${hour}:${minute}:00`,
    `${year}-${month}-${day} ${hour}:${minute}:${second}`
  ]
}

export function ymdhmsFormatter (type, value) {
  if (type === 'year') {
    return `${value}年`
  } else if (type === 'month') {
    return `${value}月`
  } else if (type === 'day') {
    return `${value}日`
  } else if (type === 'hour') {
    return `${value}时`
  } else if (type === 'minute') {
    return `${value}分`
  } else if (type === 'second') {
    return `${value}秒`
  }
  return value
}

export function getQueryString (url) {
  url = url == null ? window.location.href : url
  const search = url.substring(url.lastIndexOf('?') + 1)
  const obj = {}
  const reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, (rs, $1, $2) => {
    const name = decodeURIComponent($1)
    let val = decodeURIComponent($2)
    val = String(val)
    if (val.indexOf('#/') > -1) {
      val = val.substring(0, val.indexOf('#'))
    }
    obj[name] = val
    return rs
  })
  return obj || {}
}

export function getRole (userInfo) {
  // 企业用户
  if (userInfo.deptId === 102) {
    // 企业用户
    return 'government'
  }
  return 'enterprise'
}

export default {
  dateValueFormat,
  ymdhmsFormatter,
  getQueryString,
  getRole
}
